package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Vector;


public class ConsoleHandler implements Runnable{
	private Vector<ClientRequestProcessor> alleClients;
	private Vector<Game> alleGames;
	private Vector<String> commands;
	private LabyrinthServer server;
	
	public ConsoleHandler(Vector<ClientRequestProcessor> alleClients, Vector<Game> alleGames, LabyrinthServer server) {
		this.alleClients = alleClients;
		this.alleGames = alleGames;
		this.server = server;
		this.commands = new Vector<String>();
		
		commands.add("connections");
		commands.add("games");
		commands.add("shutdown");
	}
	
	public void run() {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String input = "";
		while(input != null) {
			try {
				input = in.readLine();
				
				if(input != null && (input.equals("") || input.equals("help"))) {
					// Commands ausgeben
					System.out.println("Valid commands are:");
					Iterator<String> iter = commands.iterator();
					while(iter.hasNext()) {
						System.out.println(iter.next());
					}
				} else if(input != null && (input.equals("connections") || input.equals("con"))) {
					System.out.println("Number of connections: " + alleClients.size());
				} else if(input != null && (input.equals("games"))) {
					System.out.println("Number of active games: " + alleGames.size());
					System.out.println("Number of active players: " + server.getClientsInGames());
				} else if(input != null && input.equals("shutdown")) {
					Iterator<ClientRequestProcessor> iter = alleClients.iterator();
					while(iter.hasNext()) {
						iter.next().timeout();
					}
					server.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
